package com.company.huisuo.biz.adapter

import com.company.huisuo.base.BaseMultiAdapter
import com.company.huisuo.databinding.ItemRechargeCustomBinding
import com.company.huisuo.databinding.ItemRechargeMoneyBinding


class MemberRechargeAdapter(val moneyClick: (Int) -> Unit, val customClick: () -> Unit) :
    BaseMultiAdapter<Int>() {

    private var mSelectPos = -1

    companion object {
        private const val TYPE_MONEY = 1
        private const val TYPE_CUSTOM = 2
    }

    override fun registerItemTypes(): List<Int> {
        return arrayListOf(TYPE_MONEY, TYPE_CUSTOM)
    }

    override fun createViewHolder(itemType: Int) {
        when (itemType) {
            TYPE_MONEY -> createMoney()
            TYPE_CUSTOM -> createCustom()
        }
    }

    override fun dispatchItemType(data: Int, position: Int): Int {
        return if (position == 8) {
            TYPE_CUSTOM
        } else {
            TYPE_MONEY
        }
    }

    private fun createMoney() {
        TYPE_MONEY.registerItem(createBinding = ItemRechargeMoneyBinding::inflate,
            convert = { holder, position, item ->
                holder.binding.apply {
                    tvTop.text = "$item"
                    tvBottom.text = "$item"
                    clLayout.isSelected = mSelectPos == position
                    tvUnit.isSelected = mSelectPos == position
                    tvBottom.isSelected = mSelectPos == position
                    clLayout.setOnClickListener {
                        mSelectPos = position
                        notifyDataSetChanged()
                        moneyClick.invoke(item ?: 0)
                    }
                }
            })
    }

    private fun createCustom() {
        TYPE_CUSTOM.registerItem(createBinding = ItemRechargeCustomBinding::inflate,
            convert = { holder, _, item ->
                holder.binding.clLayout.setOnClickListener {
                    customClick.invoke()
                }
            })
    }
}